/**
 * @file user_usart.c
 * @author Victor Lau (1345236485@qq.com)
 * @brief 
 * @version 0.1
 * @date 2023-07-19
 * 
 * @copyright Copyright (c) 2023 Victor Lau. All rights reserved.
 * 
 */
#include "include.h"
#include "user_usart.h"

void uart_init(char *path)
{
        int fd;

        fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);

        if(fd == -1)
        {
                printf("\n  Error! in Opening %s ", path);
                exit(0);
        }

        struct termios SerialPortSettings;
        tcgetattr(fd, &SerialPortSettings);                                             // Get the current attributes of the Serial port
        /* Setting the Baud rate */
        cfsetispeed(&SerialPortSettings, B38400);                         // Set Read  Speed as 38400
        cfsetospeed(&SerialPortSettings, B38400);                       // Set Write Speed as 38400
        /* 8N1 Mode */

        SerialPortSettings.c_cflag |= PARENB;                                   //偶校验
        SerialPortSettings.c_cflag &= ~PARODD;
        // SerialPortSettings.c_cflag &= ~PARENB;                          // Disables the Parity Enable bit(PARENB),So No Parity
        SerialPortSettings.c_cflag &= ~CSTOPB;                               // CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit
        SerialPortSettings.c_cflag &= ~CSIZE;                                    // Clears the mask for setting the data size*/
        SerialPortSettings.c_cflag |= CS8;                                            // Set the data bits = 8
        SerialPortSettings.c_cflag &= ~CRTSCTS;                             // No Hardware flow Control
        SerialPortSettings.c_cflag |= CREAD | CLOCAL;                  // Enable receiver,Ignore Modem Control lines
        SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);    // Disable XON/XOFF flow control both i/p and o/p
        SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Non Cannonical mode
        SerialPortSettings.c_oflag &= ~OPOST;                                // No Output Processing   raw  format  output
        
        /* Setting Time outs */
        SerialPortSettings.c_cc[VMIN] = 0;                                         // Read at least 10 characters
        SerialPortSettings.c_cc[VTIME] = 0;                                       // Wait indefinetly

        /* Set the attributes to the termios structure*/
        if ((tcsetattr(fd, TCSANOW, &SerialPortSettings)) != 0)
                printf("\n  ERROR ! in Setting attributes");
        else
                printf("\n  BaudRate = 38400 \n  StopBits = 1 \n  Parity   = none");

        tcflush(fd, TCIFLUSH);                                                                //  Discards old data in the rx buffer*/
        close(fd);                                                                                          //  Close the serial port */
        usleep(40000);                                                                               // 40ms
}
